import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class MUtils {
  /// 将普通颜色值转为flutter所需颜色值，例：#fff =>
  static int convertColor(String hexColor) {
    hexColor = hexColor.replaceAll("#", "");
    if (hexColor.length == 6) {
      hexColor = "FF$hexColor";
    }
    return int.parse(hexColor, radix: 16);
  }

  /// 随机生成图标值
  static int generateRandomEHex(int length) {
    var random = Random();
    var hex = '0xe';
    for (var i = 2; i < length; i++) {
      hex += random.nextInt(16).toRadixString(16);
    }
    return int.parse(hex);
  }

  static void showCupertinoAlertDialog(
    BuildContext context, {
    String title = "提示",
    String content = "",
    // 确定回调
    Function? confirm,
    // 取消回调
    Function? cancel,
    String confirmText = "确定",
    String cancelText = "取消",
    // 所有事件结束后的回调
    Function? callback,
  }) {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return CupertinoAlertDialog(
            title: Text(title!),
            content: Column(
              children: <Widget>[
                const SizedBox(
                  height: 10,
                ),
                Align(
                  alignment: const Alignment(0, 0),
                  child: Text(content),
                ),
              ],
            ),
            actions: <Widget>[
              CupertinoDialogAction(
                child: Text(cancelText),
                onPressed: () async {
                  if (cancel != null) {
                    await cancel();
                  }
                  if (context.mounted) Navigator.of(context).pop(false);
                },
              ),
              CupertinoDialogAction(
                child: Text(confirmText),
                onPressed: () async {
                  if (confirm != null) {
                    await confirm();
                  }
                  if (context.mounted) Navigator.of(context).pop(true);
                },
              ),
            ],
          );
        }).then((value) => {
          if (callback != null) {callback(value)}
        });
  }
}
